//
//  search.swift
//  RequestLib
//
//  Created by meicam on 2020/10/9.
//  Copyright © 2020 刘东旭. All rights reserved.
//

import Foundation

struct Info {
    var flag = 0
    var id = "0"
    var title = ""
    var img = ""
}

struct SearchInfo: Parsable {
    var success = 0
    var code = 0
    var info = [Info]()
    var title = ""
    init(data: Data) {
        var jsonData = data
        jsonData.removeFirst()
        jsonData.removeLast()
        jsonData.removeLast()
        guard let obj = try? JSONSerialization.jsonObject(with: jsonData, options: [.mutableLeaves,.mutableContainers]) as? [String:Any] else {
            return
        }
        guard let title = obj["title"] as? String else {
            return
        }
        guard let success = obj["success"] as? Int else {
            return
        }
        guard let code = obj["code"] as? Int else {
            return
        }
        guard let infos = obj["info"] as? Array<Any> else {
            return
        }
        var array = Array<Info>()
        for i in 0..<infos.count {
            let dic = infos[i] as! [String: Any]
            var info = Info()
            info.flag = dic["flag"] as? Int ?? 0
            info.id = dic["id"] as? String ?? ""
            info.title = dic["title"] as? String ?? ""
            info.img = dic["img"] as? String ?? ""
            array.append(info)
        }
        self.title = title
        self.success = success
        self.code = code
        self.info = array
    }
}

struct SearchInfo1: LDXConvertible {
    init(_ data: Data?) {
        var jsonData = data
        jsonData!.removeFirst()
        jsonData!.removeLast()
        jsonData!.removeLast()
        guard let obj = try? JSONSerialization.jsonObject(with: jsonData!, options: [.mutableLeaves,.mutableContainers]) as? [String:Any] else {
            return
        }
        guard let title = obj["title"] as? String else {
            return
        }
        guard let success = obj["success"] as? Int else {
            return
        }
        guard let code = obj["code"] as? Int else {
            return
        }
        guard let infos = obj["info"] as? Array<Any> else {
            return
        }
        var array = Array<Info>()
        for i in 0..<infos.count {
            let dic = infos[i] as! [String: Any]
            var info = Info()
            info.flag = dic["flag"] as? Int ?? 0
            info.id = dic["id"] as? String ?? ""
            info.title = dic["title"] as? String ?? ""
            info.img = dic["img"] as? String ?? ""
            array.append(info)
        }
        self.title = title
        self.success = success
        self.code = code
        self.info = array
    }
    
    var success = 0
    var code = 0
    var info = [Info]()
    var title = ""
}
